/*
 * ConstantBoolean.cpp
 *
 *  Created on: Feb 21, 2010
 *      Author: dhovemey
 */

#include <cassert>
#include "util/DataInputStream.h"
#include "util/DataOutputStream.h"
#include "vm/IConstantVisitor.h"
#include "vm/ConstantBoolean.h"

ConstantBoolean::ConstantBoolean()
	: Constant(CONSTANT_BOOLEAN)
	, m_value(false)
{
}

ConstantBoolean::ConstantBoolean(bool value)
	: Constant(CONSTANT_BOOLEAN)
	, m_value(value)
{
}

ConstantBoolean::~ConstantBoolean()
{

}

uint8_t ConstantBoolean::getTagByte() const
{
	return CONSTANT_BOOLEAN_TAG;
}

std::wstring ConstantBoolean::asString(ConstantPool *cp) const
{
	return m_value ? L"true" : L"false";
}

void ConstantBoolean::accept(IConstantVisitor *visitor)
{
	visitor->visitConstantBoolean(this);
}

bool ConstantBoolean::compare(const Constant *other) const
{
	assert(other->getType() == CONSTANT_BOOLEAN);
	return (m_value ? 1 : 0) < (static_cast<const ConstantBoolean *>(other)->m_value ? 1 : 0);
}

void ConstantBoolean::read(DataInputStream *in)
{
	m_value = (in->readByte() != 0);
}

void ConstantBoolean::write(DataOutputStream *out) const
{
	out->writeByte(uint8_t(m_value ? 1 : 0));
}
